#include "httpUtils.h"

String http_getData(String name){

      String payload="";
      HTTPClient http;//定义了一个能访问http的客户端

      http.begin(name); //HTTP

      int httpCode = http.GET();

      if(httpCode > 0) {//code==200才是正常

          if(httpCode == HTTP_CODE_OK) {
              payload = http.getString();//接收返回的数据
              
              //Serial.println(payload);
          }
      } else {
          Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();

    return payload;

}

void getWeatherCityName(String data){


    cJSON *root=cJSON_Parse(data.c_str());
      if(root == NULL){

          Serial.println("返回数据错误");
      }else{
          cJSON*result=cJSON_GetObjectItem(root,"results");
          cJSON*resultAry=cJSON_GetArrayItem(result,0);
          cJSON* location=cJSON_GetObjectItem(resultAry,"location");
          cJSON* now=cJSON_GetObjectItem(resultAry,"now");
          cJSON* last_update=cJSON_GetObjectItem(resultAry,"last_update");
          cJSON*name=cJSON_GetObjectItem(location,"name");
          cJSON*text=cJSON_GetObjectItem(now,"text");
          Serial.println("城市:"+String(name->valuestring));
          Serial.println("天气:"+String(text->valuestring));
          Serial.println("更新时间:"+String(last_update->valuestring));
         // Serial.printf("地址：%s\n天气：%S\n更新时间：%S",name->valuestring,text->valuestring,timed->valuestring);
      }
}